package HTML::FormFu::Element::Select::DBIC;
use strict;

BEGIN {
    use vars qw($VERSION );
    $VERSION     = '0.01';
}

use base 'HTML::FormFu::Element::Select';
use Carp qw/ croak /;

__PACKAGE__->mk_accessors( qw/
    _dbic_args
/ );

sub dbic_args {
    my ( $self, $args )  = @_;
    if ( ! $args ){
        return $self->_dbic_args();
    }
    $self->_dbic_args( $args );
    my $stash = $self->form->{stash};
    my $schema = $stash->{'schema'};
    my $resultset_name = $args->{resultset_name}; # || ucfirst $self->name;
    croak "Needs a DBIx::Class schema and a ResultSet class name" 
    if ( ! $schema) || ( ! $resultset_name );
    my $rs = $schema->resultset( $resultset_name )->search( $args->{where},
        $args->{search_attr} );
    my ($key) = $schema->resultset( $resultset_name )->result_source->primary_columns;
#    my $key = $args->{key} || 'id';
    my $presentation = $args->{presentation} || 'id';
    my @options = map { [ $_->$key, $_->$presentation ] } $rs->all;
    $self->options( \@options );
}

1;
# The preceding line will help the module return a true value


#################### subroutine header begin ####################

=head2 sample_function

 Usage     : How to use this function/method
 Purpose   : What it does
 Returns   : What it returns
 Argument  : What it wants to know
 Throws    : Exceptions and other anomolies
 Comment   : This is a sample subroutine header.
           : It is polite to include more pod and fewer comments.

See Also   : 

=cut




#################### main pod documentation begin ###################
## Below is the stub of documentation for your module. 
## You better edit it!


=head1 NAME

HTML::FormFu::Element::Select::DBIC - Module abstract (<= 44 characters) goes here

=head1 SYNOPSIS

  use HTML::FormFu::DBIC;
  blah blah blah


=head1 DESCRIPTION

Stub documentation for this module was created by ExtUtils::ModuleMaker.
It looks like the author of the extension was negligent enough
to leave the stub unedited.

Blah blah blah.


=head1 USAGE



=head1 BUGS



=head1 SUPPORT



=head1 AUTHOR

    Zbigniew Lukasiak
    CPAN ID: ZBY
    zby@cpan.org
    http://perlalchemy.blogspot.com

=head1 COPYRIGHT

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.


=head1 SEE ALSO

perl(1).

=cut

#################### main pod documentation end ###################


1;
# The preceding line will help the module return a true value

